#!/bin/bash
#
# Travis build [before_install] procedures
#
#  Trevor SANDY <trevor.sandy@gmail.com>
#  Last Update: Sep 14, 2019
#  Copyright (c) 2017 - 2020 by Trevor SANDY
#

# Skip build if the commit message contains [skip travis] or [travis skip]
# or environment variable LP3D_SKIP_TRAVIS_VAR is true
echo "$TRAVIS_COMMIT_MESSAGE" | awk '{print tolower($0)}' | grep -E '\[(skip travis|travis skip)\]' \
&& echo "[skip travis] detected, exiting." \
&& exit 0 || [ "$LP3D_SKIP_TRAVIS_VAR" = "true" ] \
&& echo "'Skip Travis-CI' detected in environment variable." \
&& exit 0 || true

# Build package if commit message contains [build pkg] 
# or environment variable LP3D_BUILD_PKG_VAR is true
echo "$TRAVIS_COMMIT_MESSAGE" | awk '{print tolower($0)}' | grep -E '\[build pkg.*\]' \
&& export LP3D_BUILD_PKG=yes \
&& echo "[build pkg] detected in commit message." || [ "$LP3D_BUILD_PKG_VAR" = "true" ] \
&& export LP3D_BUILD_PKG=yes \
&& echo "'Build package' detected in environment variable." || true

# Deploy package if environment variable LP3D_TEST_SF_DEPLOY_VAR is true
[ "$LP3D_TEST_SF_DEPLOY_VAR" = "true" ] \
&& echo "'Test Sourceforge deploy' detected in environment variable." || true

# Skip Sourceforge deploy if LP3D_SKIP_SF_DEPLOY_VAR is true
[ "$LP3D_SKIP_SF_DEPLOY_VAR" = "true" ] \
&& export LP3D_SKIP_SF_DEPLOY=yes \
&& echo "'Skip Sourceforge deploy' detected in environment variable." || true;

# Deploy package if pushed tag exists and has a valid format (vN.N.N[_A])
# Automatically deploy to GitHub Release and Sourceforge.net.
# Skip Sourceforge.net if git annotation contains [skip sfr]
LP3D_VALID_DEPLOY_TAG=$(echo "$TRAVIS_TAG" | perl -nle 'print "yes" if m{^(?!$)(?:v[0-9]+\.[0-9]+\.[0-9]+_?[^\W]*)?$} || print "no"')
if [ "$LP3D_VALID_DEPLOY_TAG" = "yes" ]; then
  echo "Deploy tag detected.";
  echo "Deploy tag is $TRAVIS_TAG.";
  echo "Deploy branch is $TRAVIS_BRANCH.";
  export LP3D_BUILD_PKG=yes;
  export LP3D_DEPLOY_PKG=yes;
  echo $(git tag -l -n $TRAVIS_TAG | awk '{print tolower($0)}') | grep -E '\[skip sfr\]' \
  && export LP3D_SKIP_SF_DEPLOY=yes \
  && echo "Skip Sourceforge deploy detected." || true;
elif [ -n "$TRAVIS_TAG" ]; then
  echo "WARNING - Tag $TRAVIS_TAG detected but is not a valid Deploy Tag. Deploy ingored.";
fi

# Add private qt repositories
if [[ "$TRAVIS_OS_NAME" = "linux" && (("$LP3D_COMPILE_SOURCE" = "true" && -z "$LP3D_BUILD_PKG") || \
     ("$LP3D_BUILD_PKG" = "yes" && "$LP3D_BUILD_APPIMAGE" = "true")) && -z "$LP3D_DEPLOY_PACKAGES" ]]; then
  if [ "$LP3D_QT_BASE" = "512" ]; then sudo add-apt-repository ppa:beineri/opt-qt-5.12.3-xenial -y; fi;
fi
# Parse the commit message for 3rd party build override. As 3rd party items are not expected to change often
# we cache them to speed up the build. If/when it is necessary to build one of the items, we signa with 1 in the commit message.
# The format is [build pkg 1 1 1] to build all 3rd party itmes. The first position is LDGlite, the second is LDView and the
# third is LPub3D_Trace (POV-Ray). Alternatively you may choose to build only one of the items in which case the settings would
# be ...1 0 0 to only build LDGlite. It is also possible to override the cache when just compiling the source by entering
# 'build 3rdpkg 1 1 1' in the commit message.
if [ "$LP3D_BUILD_PKG" = "yes" ]; then
  LP3D_BUILD_3RD=$(echo "$TRAVIS_COMMIT_MESSAGE" | sed -n -e 's/^.*build pkg //p' -e 's/^.*deploy pkg //p' | sed 's/].*//g');
elif [ "$LP3D_COMPILE_SOURCE" = "true" ]; then
  LP3D_BUILD_3RD=$(echo "$TRAVIS_COMMIT_MESSAGE" | sed -n -e 's/^.*build 3rdpkg //p');
fi;
if [ -n "$LP3D_BUILD_3RD" ]; then
  export LP3D_BUILD_LDGLITE=$(echo "$LP3D_BUILD_3RD" | cut -d' ' -f 1);
  export LP3D_BUILD_LDVIEW=$(echo "$LP3D_BUILD_3RD" | cut -d' ' -f 2);
  export LP3D_BUILD_POVRAY=$(echo "$LP3D_BUILD_3RD" | cut -d' ' -f 3);
fi

# Build 3rdPart renderers from environment variable flag
[ "$LP3D_BUILD_LDGLITE_VAR" = "true" ] && export LP3D_BUILD_3RD=1 && export LP3D_BUILD_LDGLITE=1 \
&& echo "'Build LDGLite' detected in environment variable." || true
[ "$LP3D_BUILD_LDVIEW_VAR" = "true" ] && export LP3D_BUILD_3RD=1 && export LP3D_BUILD_LDVIEW=1 \
&& echo "'Build LDView' detected in environment variable." || true
[ "$LP3D_BUILD_POVRAY_VAR" = "true" ] && export LP3D_BUILD_3RD=1 && export LP3D_BUILD_POVRAY=1 \
&& echo "'Build POV-Ray' detected in environment variable." || true
[ "$LP3D_BUILD_MESALIB_VAR" = "true" ] && export LP3D_BUILD_3RD=1 && export LP3D_BUILD_MESALIB=1 \
&& echo "'Build Mesa Library' detected in environment variable." || true

# Update repository index and download package build dependencies if required
# Install specific library versions:
# - libPNG:   1.6.35
# - libGL2PS: 1.4.0
# - libJPEG:  9c
# - tinyXML:  2.6.2
# - miniZIP:  1.2.11
# - libTIFF:  4.0.10
# - libSDL2:  2.0.9
# - openEXR:  2.2.0
# - ilmBASE   2.2.1
if [ "$TRAVIS_OS_NAME" = "linux" ]; then
  if [[ (("$LP3D_COMPILE_SOURCE" = "true" && -z "$LP3D_BUILD_PKG") || ("$LP3D_BUILD_PKG" = "yes" && "$LP3D_BUILD_APPIMAGE" = "true")) && -z "$LP3D_DEPLOY_PACKAGES" ]]; then
    sudo apt-get update -qq;
  fi;
  export LP3D_DIST_DIR=lpub3d_linux_3rdparty;
elif [ "$TRAVIS_OS_NAME" = "osx" ]; then
  if [[ "$LP3D_COMPILE_SOURCE" = "true" && "$LP3D_BUILD_PKG" = "" ]]; then
    brew install ccache gl2ps jpeg minizip;
    LP3D_EXP_CCACHE_PATH=true;
  fi;
  if [[ "$LP3D_BUILD_MACOS" = "true" && "$LP3D_BUILD_PKG" = "yes" ]]; then
    brew install ccache
    echo "Installing specific libraries..."
    git -C "$(brew --repo homebrew/core)" fetch --unshallow && cd "$(brew --repo homebrew/core)"
    git checkout 34de10b34c58e48e6e94a3ca032d8273e822ee76 && HOMEBREW_NO_AUTO_UPDATE=1 brew reinstall libpng
    git checkout e75d4380bf29e460ab9711e1cf6e2464fbd4472e && HOMEBREW_NO_AUTO_UPDATE=1 brew install gl2ps
    git checkout ae184c6fe491b63ab7624061f4078a1a2e9db3cd && HOMEBREW_NO_AUTO_UPDATE=1 brew install jpeg
    git checkout df2f446fe209c370cfc1e32c6ac19871b32725d0 && HOMEBREW_NO_AUTO_UPDATE=1 brew install minizip
    git checkout 8170d91fcd80e6189a5fe9fa2acf13b53429e9aa && HOMEBREW_NO_AUTO_UPDATE=1 brew install libtiff
    git checkout 5b0e02212af229f2ab8ea36b7839c60789aabd35 && HOMEBREW_NO_AUTO_UPDATE=1 brew install openexr
    git checkout 63bf3ab117c43157295c7828f64a345f5d04c836 && HOMEBREW_NO_AUTO_UPDATE=1 brew install ilmbase
    git checkout 6486d2d092f79913480f5fe64504dcaa571d21f2 && HOMEBREW_NO_AUTO_UPDATE=1 brew install sdl2
    git checkout master && cd "$LP3D_BUILD_FOLDER"
    LP3D_EXP_CCACHE_PATH=true;
  fi;
  if [ "$LP3D_EXP_CCACHE_PATH" = "true" ]; then
    export PATH="/usr/local/opt/ccache/libexec:$PATH";
  fi
  export LP3D_DIST_DIR=lpub3d_macos_3rdparty;
fi
# Decrypt and extract encrypted files
if [ "$LP3D_BUILD_PKG" = "yes" ]; then
  [ "$TRAVIS_SECURE_ENV_VARS" == "false" ] || openssl aes-256-cbc -K $encrypted_ad0a5b26bc84_key -iv $encrypted_ad0a5b26bc84_iv -in ${LP3D_SECURE_DIR}/.secrets.tar.enc -out /tmp/.secrets.tar -d;
  if [ -f "/tmp/.secrets.tar" ]; then
    echo "Extracting secrets..." && tar xvf /tmp/.secrets.tar -C /tmp/;
  else
    echo "ERROR - /tmp/.secrets.tar file not found - cannot perform secure transfers";
  fi;
fi
